package com.banzhi.shop.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.R;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.entity.GoodsDetails.Buy_Data;
import com.banzhi.shop.entity.OrderAffirm.OrderAffirm;
import com.banzhi.shop.model.CallBack.OrderAffirmCallBack;
import com.banzhi.shop.model.IOrderAffirmModel;
import com.banzhi.shop.model.Impl.OrderAffirmImpl;
import com.banzhi.shop.model.Impl.OrderAffirmImpl2;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.NumberUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;
import com.banzhi.shop.view.MaxLengthWatcher;
import com.squareup.picasso.Picasso;

import org.json.JSONObject;

public class

OrderAffirmActivity2 extends BaseActivity implements View.OnClickListener, OrderAffirmCallBack {

    /**
     * 传过来的数据
     */
    private Buy_Data data;
    /**
     * 退出
     */
    private ImageView ivback;
    /**
     * 地址
     */
    private TextView tvaddress;
    /**
     * 商品数量
     */
    private TextView tvnum;
    /**
     * 合计价格
     */
    private TextView tvheji;
    /**
     * 商品价格
     */
    private TextView tvgoodsprice;
    /**
     * 最后支付的价格
     */
    private TextView tvendprice;
    /**
     * 订单信息里的图片
     */
    private ImageView ivimage;
    /**
     * 订单信息里的标题
     */
    private TextView tvtitle;
    /**
     * 订单信息里的规格
     */
    private TextView tvguige;
    /**
     * 订单信息里的数量
     */
    private TextView tvnumber;
    /**
     * 订单信息里的价格
     */
    private TextView tvprice;
    /**
     * 添加地址
     */
    private LinearLayout ll_add_address;
    /**
     * 留言
     */
    private EditText etliuyan;
    /**
     * 运费
     */
    private TextView tvyunfei;
    /**
     * 立即支付
     */
    private TextView tv_zhifu;
    private TextView tvlijian;
    private String liuyan;
    private String address;
    private String address_id;
    private String optionid;
    private String goodid;
    private String goodnum;
    private OrderAffirmImpl2 model;
    private String mType;
    private String mPrice;
    private String flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_affirm);
        Translucent.setTranslucent(this);
        Intent intent = getIntent();
        data = (Buy_Data) intent.getSerializableExtra("goods_data");
        optionid = intent.getStringExtra("optionid");
        goodid = intent.getStringExtra("goodsid");
        goodnum = intent.getStringExtra("total");
        mPrice = intent.getStringExtra("price");
        flag = intent.getStringExtra("flag");

        setinits();
        setListeners();
        showViews();
        address = data.getDefault_address().getDetail();
        address_id = data.getDefault_address().getId();
        model = new OrderAffirmImpl2();


    }

    private void showViews() {
        if (data.getDefault_address().getDetail() != null && !"".equals(data.getDefault_address().getDetail())) {
            tvaddress.setText(data.getDefault_address().getDetail());
        }
        tvnum.setText(data.getGoodsInfo().getTotal());
        if (ASUtils.isCanUse(data.getGoodsInfo().getPriceSum())) {
            tvheji.setText(NumberUtils.DecimalFormat(data.getGoodsInfo().getPriceSum()));
            tvgoodsprice.setText(NumberUtils.DecimalFormat(data.getGoodsInfo().getPriceSum()));
        }
        if (ASUtils.isCanUse(data.getGoodsInfo().getMarketprice())) {
            tvprice.setText(mPrice);
          //  LogUtils.e("price",mPrice);
          //  LogUtils.e("MarketPrice",data.getGoodsInfo().getMarketprice());
        }
        if (ASUtils.isCanUse(data.getGoodsInfo().getGoodsSum())) {
            tvendprice.setText(NumberUtils.DecimalFormat(data.getGoodsInfo().getGoodsSum()));
        }
        if (ASUtils.isCanUse(data.getGoodsInfo().getCommission())) {
            tvlijian.setText(NumberUtils.DecimalFormat(data.getGoodsInfo().getCommission()));
        }
        if (ASUtils.isCanUse(data.getGoodsInfo().getThumb())) {
            Picasso.with(MyApp.context).load(data.getGoodsInfo().getThumb()).placeholder(R.mipmap.zhanweitu2).into(ivimage);
        } else {
            ivimage.setImageResource(R.mipmap.moren);
        }
        tvtitle.setText(data.getGoodsInfo().getTitle());
        tvnumber.setText(data.getGoodsInfo().getTotal());
        if (data.getGoodsInfo().getTitle1() != null) {
            tvguige.setText(data.getGoodsInfo().getTitle1());
        }
        if (ASUtils.isCanUse(data.getDispatchprice())) {
            tvyunfei.setText(NumberUtils.DecimalFormat(data.getDispatchprice()));
        }
    }

    private void setinits() {
        ivback = (ImageView) findViewById(R.id.iv_back);
        tvaddress = (TextView) findViewById(R.id.tv_address);
        tvnum = (TextView) findViewById(R.id.tv_num);
        tvyunfei = (TextView) findViewById(R.id.tv_yunfei);
        tvheji = (TextView) findViewById(R.id.tv_heji);
        tvgoodsprice = (TextView) findViewById(R.id.tv_goosdprice);
        tvendprice = (TextView) findViewById(R.id.tv_endprice);
        ivimage = (ImageView) findViewById(R.id.iv_image);
        tvtitle = (TextView) findViewById(R.id.tv_title);
        tvguige = (TextView) findViewById(R.id.tv_guige);
        tvnumber = (TextView) findViewById(R.id.tv_number);
        tvprice = (TextView) findViewById(R.id.tv_price);
        tvlijian = (TextView) findViewById(R.id.tv_lijian);
        ll_add_address = (LinearLayout) findViewById(R.id.ll_add_address);
        tv_zhifu = (TextView) findViewById(R.id.tv_zhifu);
        etliuyan = (EditText) findViewById(R.id.et_liuyan);
        etliuyan.addTextChangedListener(new MaxLengthWatcher(50, etliuyan));
    }

    private void setListeners() {
        ivback.setOnClickListener(this);
        ll_add_address.setOnClickListener(this);
        tv_zhifu.setOnClickListener(this);
    }

    /**
     * 用于传过去的标志  表示的由选择地址进去的选择地址页面   如果是选择地址进去 选择地址后悔finish()   直接进去的啥也不干
     */
    private boolean isresult = true;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.ll_add_address:

                Intent intent = new Intent(this, ChoiceAddressActivity.class);
                intent.putExtra("isresult", isresult);
                startActivityForResult(intent, 100);
                break;
            case R.id.tv_zhifu:
                liuyan = etliuyan.getText().toString().trim();
                if ("".equals(address)) {
                    ToastUtils.makeToastLong("请先选择收货地址");
                    Intent intent1 = new Intent(this, AddAddressActivity.class);
                    intent1.putExtra("isresult", isresult);
                    startActivity(intent1);
                } else {
                    LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
                    model.zhifu(data.getGoodsInfo().getId(), data.getGoodsInfo().getTotal(), address_id, optionid, liuyan, this);
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 100:
                if (resultCode == 1) {
                    address = data.getStringExtra("address");
                    address_id = data.getStringExtra("id");
                    tvaddress.setText(address);
                    LogUtils.d("address_id", address_id + "address_id");
                    LoadingDialogUtils.showProgress(this);
                    model.buy(goodid, goodnum, optionid, address_id, this);
                }

                break;
        }

    }

    /**
     * @param object 支付成功
     */
    @Override
    public void onzhifuSuccess(Object object) {
        OrderAffirm data = (OrderAffirm) object;
  //      Intent intent = new Intent(this, CheckstandActivity.class);
        Intent intent = new Intent(this,CheckstandGroupActivity.class);
        intent.putExtra("orderid", data.getRes().getOrderInfo().getOrder_id());
        intent.putExtra("price", mPrice);
        intent.putExtra("balance", data.getRes().getBalance());
        intent.putExtra("bianma", data.getRes().getOrderInfo().getOrdersn());
        if ("3".equals(flag)) {
            intent.putExtra("flag","3");
        }
        intent.putExtra("TT","1");
        startActivity(intent);
        finish();
    }

    /**
     * @param object 支付失败
     */
    @Override
    public void onzhifuError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }

    @Override
    public void onbuySuccess(Object object) {
        Buy_Data data = (Buy_Data) object;
        if (ASUtils.isCanUse(data.getDispatchprice())) {
            tvyunfei.setText(NumberUtils.DecimalFormat(data.getDispatchprice()));
        }
        if (ASUtils.isCanUse(data.getGoodsInfo().getGoodsSum())) {
            tvendprice.setText(NumberUtils.DecimalFormat(data.getGoodsInfo().getGoodsSum()));
        }
    }

    @Override
    public void onbuyError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort("请新建地址");
        Intent intent = new Intent(OrderAffirmActivity2.this, AddAddressActivity.class);
        startActivity(intent);
    }
}
